home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / mmalloc / mmtrace.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-15  |  4.1 KB  |  161 lines

  1. /* More debugging hooks for `mmalloc'.
  2.    Copyright 1991, 1992 Free Software Foundation
  3.  
  4.    Written April 2, 1991 by John Gilmore of Cygnus Support
  5.    Based on mcheck.c by Mike Haertel.
  6.    Modified Mar 1992 by Fred Fish.  (fnf@cygnus.com)
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <stdio.h>
  23. #include "mmalloc.h"
  24.  
  25. #ifndef    __GNU_LIBRARY__
  26. extern char *getenv ();
  27. #endif
  28.  
  29. static FILE *mallstream;
  30. static char mallenv[] = "MALLOC_TRACE";
  31. static char mallbuf[BUFSIZ];    /* Buffer for the output.  */
  32.  
  33. /* Address to breakpoint on accesses to... */
  34. static PTR mallwatch;
  35.  
  36. /* Old hook values.  */
  37.  
  38. static void (*old_mfree_hook) PARAMS ((PTR, PTR));
  39. static PTR (*old_mmalloc_hook) PARAMS ((PTR, size_t));
  40. static PTR (*old_mrealloc_hook) PARAMS ((PTR, PTR, size_t));
  41.  
  42. /* This function is called when the block being alloc'd, realloc'd, or
  43.    freed has an address matching the variable "mallwatch".  In a debugger,
  44.    set "mallwatch" to the address of interest, then put a breakpoint on
  45.    tr_break.  */
  46.  
  47. static void
  48. tr_break ()
  49. {
  50. }
  51.  
  52. static void
  53. tr_freehook (md, ptr)
  54.   PTR md;
  55.   PTR ptr;
  56. {
  57.   struct mdesc *mdp;
  58.  
  59.   mdp = MD_TO_MDP (md);
  60.   fprintf (mallstream, "- %08x\n", ptr);  /* Be sure to print it first.  */
  61.   if (ptr == mallwatch)
  62.     tr_break ();
  63.   mdp -> mfree_hook = old_mfree_hook;
  64.   mfree (md, ptr);
  65.   mdp -> mfree_hook = tr_freehook;
  66. }
  67.  
  68. static PTR
  69. tr_mallochook (md, size)
  70.   PTR md;
  71.   size_t size;
  72. {
  73.   PTR hdr;
  74.   struct mdesc *mdp;
  75.  
  76.   mdp = MD_TO_MDP (md);
  77.   mdp -> mmalloc_hook = old_mmalloc_hook;
  78.   hdr = (PTR) mmalloc (md, size);
  79.   mdp -> mmalloc_hook = tr_mallochook;
  80.  
  81.   /* We could be printing a NULL here; that's OK.  */
  82.   fprintf (mallstream, "+ %08x %x\n", hdr, size);
  83.  
  84.   if (hdr == mallwatch)
  85.     tr_break ();
  86.  
  87.   return (hdr);
  88. }
  89.  
  90. static PTR
  91. tr_reallochook (md, ptr, size)
  92.   PTR md;
  93.   PTR ptr;
  94.   size_t size;
  95. {
  96.   PTR hdr;
  97.   struct mdesc *mdp;
  98.  
  99.   mdp = MD_TO_MDP (md);
  100.  
  101.   if (ptr == mallwatch)
  102.     tr_break ();
  103.  
  104.   mdp -> mfree_hook = old_mfree_hook;
  105.   mdp -> mmalloc_hook = old_mmalloc_hook;
  106.   mdp -> mrealloc_hook = old_mrealloc_hook;
  107.   hdr = (PTR) mrealloc (md, ptr, size);
  108.   mdp -> mfree_hook = tr_freehook;
  109.   mdp -> mmalloc_hook = tr_mallochook;
  110.   mdp -> mrealloc_hook = tr_reallochook;
  111.   if (hdr == NULL)
  112.     /* Failed realloc.  */
  113.     fprintf (mallstream, "! %08x %x\n", ptr, size);
  114.   else
  115.     fprintf (mallstream, "< %08x\n> %08x %x\n", ptr, hdr, size);
  116.  
  117.   if (hdr == mallwatch)
  118.     tr_break ();
  119.  
  120.   return hdr;
  121. }
  122.  
  123. /* We enable tracing if either the environment variable MALLOC_TRACE
  124.    is set, or if the variable mallwatch has been patched to an address
  125.    that the debugging user wants us to stop on.  When patching mallwatch,
  126.    don't forget to set a breakpoint on tr_break!  */
  127.  
  128. int
  129. mmtrace ()
  130. {
  131.   char *mallfile;
  132.   int rtnval;
  133.   
  134. #if 0    /* FIXME!  This is disabled for now until we figure out how to
  135.        maintain a stack of hooks per heap, since we might have other
  136.        hooks (such as set by mmcheck) active also. */
  137.  
  138.   mallfile = getenv (mallenv);
  139.   if (mallfile  != NULL || mallwatch != NULL)
  140.     {
  141.       mallstream = fopen (mallfile != NULL ? mallfile : "/dev/null", "w");
  142.       if (mallstream != NULL)
  143.     {
  144.       /* Be sure it doesn't mmalloc its buffer!  */
  145.       setbuf (mallstream, mallbuf);
  146.       fprintf (mallstream, "= Start\n");
  147.       old_mfree_hook = mdp -> mfree_hook;
  148.       mdp -> mfree_hook = tr_freehook;
  149.       old_mmalloc_hook = mdp -> mmalloc_hook;
  150.       mdp -> mmalloc_hook = tr_mallochook;
  151.       old_mrealloc_hook = mdp -> mrealloc_hook;
  152.       mdp -> mrealloc_hook = tr_reallochook;
  153.     }
  154.     }
  155.  
  156. #endif    /* 0 */
  157.  
  158.   return (1);
  159. }
  160.  
  161.